/**
 * @author   zhangyp
 * @created  2013-8-19 上午9:18:24
 * @email    zviolet@163.com
 * TODO      利用gps定位或wifi定位当前所在位置(百度地图)
 */
package com.bible.fire.pad.activity.aspect;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.map.MKMapTouchListener;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MKOLUpdateElement;
import com.baidu.mapapi.map.MKOfflineMap;
import com.baidu.mapapi.map.MKOfflineMapListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.bible.fire.R;
import com.bible.fire.api.baidumap.BaiduMapMKTouchListener;
import com.bible.fire.api.baidumap.BaiduMapMKViewListener;
import com.bible.fire.api.baidumap.OverlayItem4BMap;
import com.bible.fire.local.AppContext;
import com.bible.fire.pad.activity.BaseActivity;
import com.bible.fire.util.AndroidUtil;
import com.bible.fire.util.SystemConstant;

public class BDGPSLocateActivity extends BaseActivity {
    
	private MapView mMapView = null;
	private MKOfflineMap mOffline = null;
	private MapController mMapController = null;
	
	/**
	 * 用于截获屏坐标
	 */
	MKMapTouchListener mapTouchListener = null;
	
	/**
	 *  MKMapViewListener 用于处理地图事件回调
	 */
	MKMapViewListener mMapListener = null;
	
	private Button saveScreenButton = null;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        this.setContentView( R.layout.baidu_locate );
        mMapView = (MapView)findViewById( R.id.bmapView );
        mMapView.getController().enableClick(true);
        mMapView.getController().setZoom(14);
        mMapView.displayZoomControls(true);
        mMapView.setDoubleClickZooming(true);
        
        mMapController = mMapView.getController();
        
      //offline地图初始化
        mOffline = new MKOfflineMap();       
        mOffline.init(mMapController, new MKOfflineMapListener() {  
            @Override  
            public void onGetOfflineMapState(int type, int state) {  
                switch (type) {  
	                case MKOfflineMap.TYPE_DOWNLOAD_UPDATE:  
	                    {  
	                        MKOLUpdateElement update = mOffline.getUpdateInfo(state);  
	                        //mText.setText(String.format("%s : %d%%", update.cityName, update.ratio));  
	                    }  
	                    break;  
	                case MKOfflineMap.TYPE_NEW_OFFLINE:  
	                    Log.d("百度离线地图", String.format("新安装%d个离线地图", state));  
	                    break;  
	                case MKOfflineMap.TYPE_VER_UPDATE:  
	                    Log.d("百度离线地图", String.format("有离线地图更新"));  
	                    break;  
	            }      
            }  
          }  
        );

        initListener();
        int num = mOffline.scan();
        Toast.makeText( this, String.format("已经安装%d个离线地图包", num), Toast.LENGTH_LONG).show();
        
        double[] positions = AndroidUtil.getCurrentLocation();
        if( positions == null ){
        	positions = SystemConstant.GPS_DEFAULT;
        }
        if( positions != null ){
        	//GeoPoint gpt = new GeoPoint((int)(33.77065*1e6), (int)(120.264914*1e6));
        	GeoPoint gpt = new GeoPoint((int)(positions[0]*1e6), (int)(positions[1]*1e6));
            mMapController.setCenter( gpt );
            mMapView.getOverlays().clear();
            Drawable marker = getResources().getDrawable(R.drawable.icon_markf);  //得到需要标在地图上的资源
    		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());   //为maker定义位置和边界		
        	mMapView.getOverlays().add( new OverlayItem4BMap(marker, mMapView, this, gpt, "当前位置") );
        	mMapView.refresh();
        }else{
        	Toast.makeText(this, "无法获取当前位置，请确认gps信息是否正常", Toast.LENGTH_LONG).show();
        }
	}
	
	private void initListener() {
		mapTouchListener = new BaiduMapMKTouchListener();
		mMapView.regMapTouchListner( mapTouchListener );
		mMapListener = new BaiduMapMKViewListener();
		mMapView.regMapViewListener(AppContext.getInstance().mBMapManager, mMapListener);
		
		saveScreenButton  = (Button)findViewById(R.id.savescreen);
		OnClickListener onClickListener = new OnClickListener(){
			@Override
			public void onClick(View v) {
				 mMapView.getCurrentMap();	
			     Toast.makeText(BDGPSLocateActivity.this, "正在截取屏幕图片...", Toast.LENGTH_SHORT ).show();
			}		
		};
		saveScreenButton.setOnClickListener( onClickListener );
	}
	
	@Override
    protected void onDestroy() {
		mOffline.destroy();
		if( mMapView != null ){
			mMapView.destroy();
		}
		super.onDestroy();
	}
	
	@Override
    protected void onPause(){
        mMapView.onPause();
        super.onPause();
    }
    
    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState){
    	super.onSaveInstanceState(outState);
    	mMapView.onSaveInstanceState(outState);    	
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
    	super.onRestoreInstanceState(savedInstanceState);
    	mMapView.onRestoreInstanceState(savedInstanceState);
    }
}
